/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.ppm.dat.view;

import bgu.ppm.dat.model.Table;
import bgu.ppm.dat.view.PivotTableWorkbunchView.TableVisualizationFactory;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Image;
import javax.swing.JComponent;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.MultiplePiePlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.category.CategoryDataset;
import org.jfree.util.TableOrder;

/**
 *
 * @author User
 */
public class PivotPieChartView extends javax.swing.JPanel implements TableVisualizationFactory {

    /**
     * Creates new form PivotLineChartView
     */
    public PivotPieChartView() {
        initComponents();
    }

    private JFreeChart createChart(CategoryDataset categorydataset) {
        JFreeChart jfreechart = ChartFactory.createMultiplePieChart("Multiple Pie Chart", categorydataset, TableOrder.BY_ROW, true, true, false);
        MultiplePiePlot multiplepieplot = (MultiplePiePlot) jfreechart.getPlot();
        JFreeChart jfreechart1 = multiplepieplot.getPieChart();
        PiePlot pieplot = (PiePlot) jfreechart1.getPlot();
        pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));
        pieplot.setLabelFont(new Font("SansSerif", 0, 8));
        return jfreechart;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    @Override
    public JComponent getComponent(Table table) {
        removeAll();

        add(new ChartPanel(createChart(DatasetUtils.createDataset(table))), BorderLayout.CENTER);

        return this;
    }

    @Override
    public String getVisualName() {
        return "Line chart";
    }

    @Override
    public Image getIcon() {
        return null;
    }

    @Override
    public String getDescription() {
        return "Line chart";
    }
}
